import axios from '@/utils/request'
import { IListResult, IPageResult } from './types/base'
import {
  IAddDepartment,
  IDepartment,
  IDepartmentUser,
} from './types/department'
import { IRole } from './types/role'

// 获取部门树
export function getDepartments() {
  return axios<IListResult<IDepartment>>({
    url: '/api/identity/ou/all/tree',
    method: 'get',
  })
}
// 新增部门
export function addDepartment(data: IAddDepartment) {
  return axios<IAddDepartment>({
    url: '/api/identity/ou',
    method: 'post',
    data,
  })
}
// 删除部门
export function deleteDepartment(ouId: string) {
  return axios<IAddDepartment>({
    url: `/api/identity/ou/${ouId}`,
    method: 'delete',
  })
}
// 修改部门
export function updateDepartment(ouId: string, data: IAddDepartment) {
  return axios({
    url: `/api/identity/ou/${ouId}`,
    method: 'put',
    data,
  })
}

// 查询部门详情
export function getDepartment(ouId: string) {
  return axios<IAddDepartment>({
    url: `/api/identity/ou/${ouId}`,
    method: 'get',
  })
}

// 获取已添加的用户列表
export function getAddedUsers(currentPage = 1, take = 10, ouId: string) {
  return axios<IPageResult<IDepartmentUser>>({
    url: `/api/identity/ou/${ouId}/user/added-list`,
    method: 'get',
    params: {
      skipCount: take * (currentPage - 1),
      maxResultCount: take,
    },
  })
}

// 获取未添加的用户列表
export function getUnAddedUsers(
  currentPage = 1,
  take = 10,
  params: {
    ouId: string
    filter: string
  },
) {
  return axios<IPageResult<IDepartmentUser>>({
    url: `/api/identity/ou/${params.ouId}/user/unadded-list`,
    method: 'get',
    params: {
      skipCount: take * (currentPage - 1),
      maxResultCount: take,
      filter: params.filter,
    },
  })
}
// 给部门添加用户
export function addUserToDepartment(userId: string, ouId: string) {
  return axios({
    url: `/api/identity/ou/${ouId}/user/${userId}`,
    method: 'post',
  })
}
// 从部门移除用户
export function removeUserFromDepartment(userId: string, ouId: string) {
  return axios({
    url: `/api/identity/ou/${ouId}/user/${userId}`,
    method: 'delete',
  })
}

// 获取已添加的角色列表
export function getAddedRoles(currentPage = 1, take = 10, ouId: string) {
  return axios<IPageResult<IRole>>({
    url: `/api/identity/ou/${ouId}/role/added-list`,
    method: 'get',
    params: {
      skipCount: take * (currentPage - 1),
      maxResultCount: take,
    },
  })
}

// 获取未添加的角色列表
export function getUnAddedRoles(currentPage = 1, take = 10, ouId: string) {
  return axios<IPageResult<IRole>>({
    url: `/api/identity/ou/${ouId}/role/unadded-list`,
    method: 'get',
    params: {
      skipCount: take * (currentPage - 1),
      maxResultCount: take,
    },
  })
}

// 给部门添加角色
export function addRoleToDepartment(roleId: string, ouId: string) {
  return axios({
    url: `/api/identity/ou/${ouId}/role/${roleId}`,
    method: 'post',
  })
}

// 从部门移除角色
export function removeRoleFromDepartment(roleId: string, ouId: string) {
  return axios({
    url: `/api/identity/ou/${ouId}/role/${roleId}`,
    method: 'delete',
  })
}
